/*
 * $RCSfile: Processor_Seta_45.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-16  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.i366.processor;

import java.sql.SQLException;
import java.util.Map;

import org.apache.log4j.Logger;

import com.i366.Constant;
import com.i366.cache.Cache;
import com.i366.room.model.RoomPersion;
import com.i366.room.model.impl.Room;
import com.i366.service.RoomService;
import com.work.client.util.I366ClientPickUtil;
import com.work.comm.util.I366PickUtil;
import com.work.db.UserInfoDao;
import com.work.db.imp.UserInfoDaoImp;
import com.work.db.model.UserInfo;
import com.work.engine.protocal.request.Request;
import com.work.engine.server.handler.Handler;

/**
 * 玩家操作坐下 站起 离开
 *
 * <p>Title: Processor_Seta_45</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class Processor_45_Seta extends Handler{
	private Logger logger = Logger.getLogger(Processor_45_Seta.class);
	@Override
	public byte[] handleRequest() {
		long l = System.currentTimeMillis();
		logger.debug("Processor_45_Seta id ");
		Request request = getGameRequest();
		UserInfo userInfo = RoomService.updateUser(request);
		if(request.getUserId() < 1000000) {
			return null;
		}
		logger.debug("Processor_45_Seta id " + request.getUserId());
		int[][] int2 = {{60,I366ClientPickUtil.TYPE_INT_1},
				{61,I366ClientPickUtil.TYPE_INT_1},
				{131,I366ClientPickUtil.TYPE_INT_4},
				{130,I366ClientPickUtil.TYPE_INT_4}};
		Map<Integer, Object> map = I366ClientPickUtil.pickAll(request.getBt(), int2);
		int action = (Integer)map.get(60);//
		int seatId = (Integer)map.get(61); //座位号
		int roomId = (Integer)map.get(131);
		int roomPath = (Integer)map.get(130);
		logger.debug(" Processor_45_Seta action :" + action + " seatId :" +seatId + " roomId:" + roomId + " roomPath :" + roomPath);
		Room room = Cache.getRoom(roomId, roomPath);
		if (room == null){
			if (action == 3) {
				if (userInfo == null) {
					UserInfoDao userInfoDao = new UserInfoDaoImp();
					try {
						userInfo = userInfoDao.getUserInfo(request.getUserId());
					} catch (SQLException e) {
						e.printStackTrace();
						return null;
					}
				}
				Object[][] objs = {
						{60,0,I366ClientPickUtil.TYPE_INT_1} ,
						{61,action,I366ClientPickUtil.TYPE_INT_1} ,
						{130,0,I366ClientPickUtil.TYPE_INT_4},
						{131,userInfo.getChip(),I366ClientPickUtil.TYPE_INT_4}
				};
				byte[] bytes = I366PickUtil.packAll(objs, Constant.REQ_GAME_SEND_SEAT_ACTION);
				return bytes;
			}
			return null;
		}
		switch (action) {
			case 1:
				logger.debug("坐下房间:" + roomId + " userId:" + request.getUserId() + " seatId :" + seatId);

				int b = room.getRoomService().downRoom(request.getUserId(), seatId , userInfo);
				logger.debug("------b :" + b + " action : " + action);
				if (b > -1) {
					return pusUser(b , null , action);
				}
				logger.debug(".....坐下用去时间: " + (System.currentTimeMillis() - l));
				return null;
			case 2:
				//站起
				RoomPersion rp = room.getRoomPersions()[seatId];
				if (rp == null || rp.getOnlinerType() == -1) {
					rp = room.getDdRoomPersions()[seatId];
				}
				if (rp == null || rp.getUserId() !=  request.getUserId()) {
					return pusUser(1 , null , action);
				}

				//如果玩家还在游戏中不给予站起
				if (rp.getType() == 3) {
					return pusUser(1 , null , action);
				}

				room.getRoomService().zhanqi(rp);
//			return pusUser(0 , rp , action);
				logger.debug(".....站起用去时间: " + (System.currentTimeMillis() - l));
				return null;
			case 3:
				//离开房间
				RoomPersion persion = null;
				int type2 = -1;
				if (seatId > -1 && seatId < 8) {
					persion = room.getRoomPersions()[seatId];
					if (persion == null  || persion.getOnlinerType() == -1) {
						type2 = 99;
						persion = room.getDdRoomPersions()[seatId];
					}
				}
				//玩家还未坐下座位
				if (persion == null) {
					return pusUser(0 , room.getRoomService().noDownLikai(request.getUserId()) , action);
				}

				//不是本人不能离开
				if (persion.getUserId() !=  request.getUserId()) {
					return pusUser(1 , null , action);
				}
				room.getRoomService().likai(persion, type2);
//			return pusUser(0 , persion , action);
				logger.debug("....离开用去时间: " + (System.currentTimeMillis() - l));
				return null;
			default:
				return null;
		}
	}

	public byte[] pusUser(int status ,RoomPersion roomPersion , int action) {
		byte[] bytes;
		if (roomPersion == null) {
			Object[][] objs = {
					{60,status,I366ClientPickUtil.TYPE_INT_1} ,
					{61,action,I366ClientPickUtil.TYPE_INT_1}
			};
			bytes = I366PickUtil.packAll(objs, Constant.REQ_GAME_SEND_SEAT_ACTION);
		}else {

			Object[][] objs = {
					{60,status,I366ClientPickUtil.TYPE_INT_1} ,
					{61,action,I366ClientPickUtil.TYPE_INT_1} ,
					{130,roomPersion.getNowcounma(),I366ClientPickUtil.TYPE_INT_4},
					{131,roomPersion.getChouma(),I366ClientPickUtil.TYPE_INT_4}
			};
			bytes = I366PickUtil.packAll(objs, Constant.REQ_GAME_SEND_SEAT_ACTION);
		}
		return bytes;
	}

	public Object makeObject() throws Exception {
		return new Processor_45_Seta();
	}

}

